<?php
/**
 * Crc CMS
 *
 *
 * @category   Crc
 * @package    Crc_Form
 * @author     Moldovan Calin <mcalin45@gmail.com>
 * @author     Vlad Razvan <razvanvlad1982@yahoo.com>
 * @copyright  Copyright (c) 2012
 * @version    $Id: Form.php 64 2013-02-14 17:20:38Z mcalin45@gmail.com $
 */


/**
 * Class for generating forms.
 *
 * @category   Crc
 * @package    Crc_Form
 * @author     Moldovan Calin <mcalin45@gmail.com>
 * @author     Vlad Razvan <razvanvlad1982@yahoo.com>
 * @copyright  Copyright (c) 2012
 */
abstract class Crc_Form extends Zend_Form
{
    /**
     * Constructor
     *
     * Registers options for the form
     *
     * @param array $options
     * @return void
     */
    public function __construct($options = array())
    {
        $this->setOptions($options);
        $this->setIsArray(true);

        parent::__construct($options);
    }

     /**
     * Set custom option for the form
     *
     * @param  string $key
     * @param  mixed $value
     * @return void
     */
    public function setCustomValue($key, $value)
    {
        if (property_exists(get_class($this), '_' . $key))
        {
            $this->{'_' . $key} = $value;
        }
    }

    /**
     * Get custom option for the form
     *
     * @param  string $key
     * @return mixed
     * @throws Exception for invalid option
     */
    public function getCustomValue($key)
    {
        if (property_exists(get_class($this), '_' . $key))
        {
            return $this->{'_' . $key};
        }
        else
        {
            throw new Exception('Property does not exist');
        }
    }

    /**
     * Add subform to the form
     *
     * @param  string $formName
     * @param  array $options
     * @return void
     */
    public function addSubForm(Zend_Form $form, $subFormName, $options = null)
    {
        $formName = (empty($options['name'])) ? $subFormName : $options['name'];
        $formSubmitName = (isset($options['submitName'])) ? $options['submitName'] : 'Save';

        $subForm = new Zend_Form_SubForm($options);
        $subForm->setName($formName);
        $subForm = $this->loadElements($subForm, $subFormName);

        parent::addSubForm($subForm, $subFormName);

        if (!isset($options['submit']))
        {
            $element = new Zend_Form_Element_Submit($formSubmitName);
            $this->addElement($element);
        }
    }
}